package com.zy.view.dialog;

import java.util.Calendar;

import zy.util.DateUtil;
import zy.util.StringUtil;

import com.zy.R;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.DatePicker.OnDateChangedListener;

public class BirthdayPickerDialog extends Dialog implements OnDateChangedListener, android.view.View.OnClickListener{
	private DatePicker datePicker;  
    private String date;  
    private TextView btn_cancel, btn_ok,dialog_title;
    private RadioButton rb_birthday_type_0,rb_birthday_type_1;
    private String initDate;
    public BirthdayPickerDialog(Context context,String initDate){
    	super(context);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_dialog_birthday);
		this.initDate = initDate;
		initViews();
		initListener();
		initDate();
		initEvents();
		setCancelable(true);
		setCanceledOnTouchOutside(true);
    }
    private void initViews() {
		datePicker = (DatePicker)findViewById(R.id.datepicker);
		dialog_title = (TextView)findViewById(R.id.dialog_title);
		btn_cancel = (TextView) findViewById(R.id.btn_cancel);
		btn_ok = (TextView) findViewById(R.id.btn_ok);
		rb_birthday_type_0 = (RadioButton)findViewById(R.id.rb_birthday_type_0);
		rb_birthday_type_1 = (RadioButton)findViewById(R.id.rb_birthday_type_1);
	}
    private void initListener(){
    	btn_cancel.setOnClickListener(this);
    }
    private void initDate(){
        Calendar calendar = Calendar.getInstance(); 
        if(StringUtil.isNotEmpty(initDate)){
        	calendar = DateUtil.parseDateString(initDate, DateUtil.FORMAT_YEAR_MON_DAY); 
        }
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), this);  
  	}
    private void initEvents() {
		onDateChanged(null, 0, 0, 0);
	}
	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		Calendar calendar = Calendar.getInstance();  
        calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        date = DateUtil.format(calendar, DateUtil.FORMAT_YEAR_MON_DAY);
        dialog_title.setText(date);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_cancel:
				if(isShowing()){
					super.dismiss();
				}
			break;
		}
	}
	public TextView getOkButton(){
		return btn_ok;
	}
	public String getDate(){
		return date;
	}
	public Integer getBirthdayType(){
		if(rb_birthday_type_0.isChecked()){
			return 0;
		}else if(rb_birthday_type_1.isChecked()){
			return 1;
		}
		return null;
	}
}
